package com.catazzz.springbootstudy.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.catazzz.springbootstudy.common.exception.Result;
import com.catazzz.springbootstudy.entity.User;
import com.catazzz.springbootstudy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @作者: Tao
 * @日期: 2024/12/26
 * @时间: 15:30
 * @描述: 用户操作类 <br><pre>
 * 包含方法:
 *      get(id) - 查询用户
 *      create(user) - 创建用户
 *      update(user) - 更新用户
 *      delete(id) - 删除用户
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户信息")
public class UserController {

    /**
     * 注入UserService对象
     */
    @Autowired
    private UserService userService;

    /**
     * 根据ID查询用户
     *
     * @param id 用户ID
     * @return 用户实体<br>
     * protocols - 指定 API 操作支持的协议（如 http、https）<br>
     * consumes - 指定 API 操作支持的请求内容类型（如 application/json）<br>
     * produces - 指定 API 操作返回的响应内容类型（如 application/json）<br>
     * httpMethod - 指定 API 操作支持的 HTTP 方法（如 GET、POST）<br>
     * tags - 指定 API 操作所属的标签, 用于分类和组织 API 文档<br>
     * response - 指定 API 操作返回的响应实体类型<br>
     */
    @ApiOperation(
            value = "根据ID查询用户",
            response = User.class,
            httpMethod = "GET",
            produces = "application/json",
            consumes = "application/json",
            protocols = "http,https"
    )
    @GetMapping("/{id}")
    public Result<User> get(@PathVariable int id) {
        return Result.success(userService.getById(id));
    }

    /**
     * 创建用户
     *
     * @param user 用户实体
     * @return 用户实体
     */
    @ApiOperation(
            value = "创建用户",
            response = User.class,
            httpMethod = "POST",
            produces = "application/json",
            consumes = "application/json",
            protocols = "http,https"
    )
    @PostMapping
    public Result<Boolean> create(User user) {
        return Result.success(userService.save(user));
    }

    /**
     * 更新用户
     *
     * @param user 用户实体
     * @return 用户实体
     */
    @ApiOperation(
            value = "更新用户",
            response = User.class,
            httpMethod = "PUT",
            produces = "application/json",
            consumes = "application/json",
            protocols = "http,https"
    )
    @PutMapping
    public  Result<Boolean> update(@RequestBody User user) {
        return Result.success(userService.updateById(user));
    }

    /**
     * 删除用户
     *
     * @param id 用户ID
     */
    @ApiOperation(
            value = "删除用户",
            httpMethod = "DELETE",
            produces = "application/json",
            consumes = "application/json",
            protocols = "http,https"
    )
    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return userService.removeById(id);
    }

    /**
     * 查询用户列表
     * @param page 页数
     * @param size 每页数量
     * @return 分页结果
     */
    @ApiOperation(
            value = "查询用户列表",
            response = User.class,
            httpMethod = "GET",
            produces = "application/json",
            consumes = "application/json",
            protocols = "http,https"
    )
    @GetMapping
    public Result<Page<User>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size
    ) {
        return Result.success(userService.page(new Page<>(page, size)));
    }

    /**
     * 根据姓名查询用户列表
     *
     * @param name 姓名
     * @return 用户列表
     */
    @ApiOperation(
            value = "根据姓名查询用户",
            response = User.class,
            httpMethod = "GET",
            produces = "application/json",
            consumes = "application/json",
            protocols = "http,https"
    )
    @GetMapping("/name")
    public Result<List<User>> getByName(@RequestParam String name) {
        return Result.success(userService.findByName(name));
    }

}
